<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<c:set var="ctx" value="${pageContext.request.contextPath}"/>
<html>
<head>
    <title></title>
	<%@include file="/WEB-INF/pages/common/basic-styles.jspf" %>
    <link rel="stylesheet" href="${ctx}/static/jquery/jquery-treetable/stylesheets/jquery.treetable.css">
    <link rel="stylesheet" href="${ctx}/static/jquery/jquery-treetable/stylesheets/jquery.treetable.theme.default.css">
    <style>
        #table th, #table td {
            font-size: 14px;
            padding : 8px;
        }
		body {
			height: 550px;
		}
    </style>
</head>
<body>

<c:if test="${not empty msg}">
<div class="alert alert-block alert-success"">
	<button type="button" class="close" data-dismiss="alert">
		<i class="icon-remove"></i>
	</button>
	<p>
		<strong>
			<i class="icon-ok"></i>
		</strong>
		${msg}
	</p>
</div>
</c:if>

<div class="operate panel panel-default" style="height:65px;">
	<div class="panel-body">
		<shiro:hasPermission name="resource:create">
			<button class="btn btn-sm btn-primary add">添加子节点</button>
		</shiro:hasPermission>
		<shiro:hasPermission name="resource:update">
            <button class="btn btn-sm btn-primary update">修改</button>
        </shiro:hasPermission>
		<shiro:hasPermission name="resource:delete">
			<button class="btn btn-sm btn-primary delete">删除</button>
		</shiro:hasPermission>
	</div>	
</div>		

<div class="table-responsive">
<form id="listForm" method="post">
<table id="table" class="table table-striped table-bordered table-hover">
    <thead>
        <tr>
        	<th class="center" width="5%">
				<label>
					<input type="checkbox" class="ace"/>
					<span class="lbl"></span>
				</label>
			</th>
            <th>名称</th>
            <th width="5%">类型</th>
            <th>URL路径</th>
            <th>权限字符串</th>
            <th width="6%">排序号</th>
        </tr>
    </thead>
    <tbody>
        <c:forEach items="${resourceList}" var="resource">
            <tr data-tt-id='${resource.id}' <c:if test="${not resource.rootNode}">data-tt-parent-id='${resource.parentId}'</c:if>>
            	<input type="hidden" id="rootNode_${resource.id}" value="${resource.rootNode}"/>
				<input type="hidden" id="type_${resource.id}" value="${resource.type}"/>
                <td class="center">
					<label>
						<input type="checkbox" class="ace" name="ids" value="${resource.id}"/>
						<span class="lbl"></span>
					</label>
				</td>
                <td>${resource.name}</td>
                <td>${resource.type.info}</td>
                <td>${resource.url}</td>
                <td>${resource.permission}</td>
                <td>${resource.orderNum}</td>
            </tr>
        </c:forEach>
    </tbody>
</table>
</form>
</div>

<%@include file="/WEB-INF/pages/common/basic-scripts.jspf" %>  
<script src="${ctx}/static/jquery/jquery-treetable/javascripts/src/jquery.treetable.js"></script>
<script>
    $(function() {
        $("#table").treetable({ expandable: true, column: 1 }).treetable("expandNode", 1);
        
        $('table th input:checkbox').on('click' , function(){
			$("input[name='ids']").prop("checked", this.checked);
		});				
		$('.alert-success').delay(3000).hide(0);
	
		$('.add').on('click', function() {
			var num = getCheckedNumByName("ids");
			if (num < 1) {
				defaultAlert("请选择父节点");
				return false;
			}
			if (num > 1) {
				defaultAlert("不能批量添加，请重新选择");
				return false;
			}				
			var id = getCheckedValueByName("ids"); 
			var type = $("#type_" + id).val();
			if (type == 'button') {
				defaultAlert("按钮不能添加子节点，请重新选择");
				return false;
			}
			location.href = '${ctx}/manage/system/resource/appendChild/' + id;
		});
		
		$('.update').on('click', function() {
			var num = getCheckedNumByName("ids");
			if (num < 1) {
				defaultAlert("请选择要修改的信息");
				return false;
			}
			if (num > 1) {
				defaultAlert("不能批量修改，请重新选择");
				return false;
			}				
			var id = getCheckedValueByName("ids"); 
			location.href = '${ctx}/manage/system/resource/update/' + id;
		});
		
		$('.delete').on('click', function() {
			var ischeck = getCheckedByName("ids");
			if (!ischeck) {
				defaultAlert("请选择要删除的信息");
				return;
			}			
			var token = false;
			$("input[type='checkbox'][name='ids']:checked").each(function() {
				var rootNode = $("#rootNode_" + $(this).val()).val();
				if (rootNode == "true") {
					token = true;
					return false;
				}
			});
			if (token) {
				defaultAlert("根节点不能删除");
				return;
			}
			defaultConfirm("确定要删除选择的节点，以及子节点吗?", function(result) {
				if (result) {
					$("#listForm").attr("action", "${ctx}/manage/system/resource/delete");
					$("#listForm").submit();
				}
	        });
		});
        
    });
</script>
</body>
</html>